package com.ce.server.controller;

import com.ce.server.common.RespBean;
import com.ce.server.pojo.Admin;
import com.ce.server.pojo.Student;
import com.ce.server.service.IAdminService;
import com.ce.server.service.IAliOssService;
import com.ce.server.service.IStudentService;
import com.ce.server.service.impl.AliOssServiceImpl;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @DATE: 2021/11/29 22:09
 * @Author: 小爽帅到拖网速
 */

@RestController
@RequestMapping("/userface")
public class AliOssController {
  @Autowired
  IAliOssService aliOssService;
  @Autowired
  IAdminService adminService;
  @Autowired
  IStudentService studentService;

  @Value("${type.admin}")
  private String type_adm;
  @Value("${type.student")
  private String type_stu;

  @ApiOperation(value = "更新用户头像")
  @PostMapping("/")
  public RespBean updateUserface(MultipartFile file, String no){
    String upload = aliOssService.upload(file);
    if (no.startsWith(type_adm)){
      Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
      currentAdmin.setUserface(upload);
      return adminService.updateAccountInfo(currentAdmin)?RespBean.success("修改成功！"):RespBean.error("修改失败！");
    }else {
      Student currentAdmin = (Student) AdminUtil.getCurrentAdmin();
      currentAdmin.setUserface(upload);
      return studentService.updateAccountInfo(currentAdmin)?RespBean.success("修改成功！"):RespBean.error("修改是失败！");
    }
  }
}
